package com.yh.csx.crm.service;

import com.github.pagehelper.PageHelper;
import com.yh.csx.crm.core.exception.BusinessException;
import com.yh.csx.crm.dao.SysOrganizationMapper;
import com.yh.csx.crm.dao.SysSalesPermissionMapper;
import com.yh.csx.crm.po.SysOrganization;
import com.yh.csx.crm.qo.OrganizationQo;
import com.yh.csx.crm.qo.SalesQo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Huang Zhaoping
 * @date 2019/4/10
 */
@Service
public class OrganizationService {

    @Autowired
    private SysOrganizationMapper organizationMapper;

    @Autowired
    private SysSalesPermissionMapper permissionMapper;

    public List<SysOrganization> findOrganizationList(OrganizationQo qo) {
        if (qo.isPageable()) {
            PageHelper.startPage(qo.getPageNum(), qo.getPageSize(), qo.isCount());
        }
        return organizationMapper.selectOrganizationList(qo);
    }

    public SysOrganization findOrganization(Integer id) {
        return organizationMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public void createOrganization(SysOrganization org) {
        String parentPath = null;
        if (org.getParentId() == null || org.getParentId() <= 0) {
            org.setParentId(0);
        } else {
            SysOrganization parent = organizationMapper.selectByPrimaryKey(org.getParentId());
            if (parent == null) throw new BusinessException("上级部门不存在");
            SalesQo qo = new SalesQo();
            qo.setOrgId(parent.getId());
            if (permissionMapper.selectSalesCount(qo) > 0) {
                throw new BusinessException("不能在包含用户的部门下新建下级部门");
            }
            parentPath = parent.getPath();
        }
        if (org.getSequence() == null) {
            org.setSequence(0);
        }
        org.setPath("");
        organizationMapper.insertSelective(org);
        org.setPath(StringUtils.isBlank(parentPath) ? String.valueOf(org.getId()) : parentPath + "," + org.getId());
        organizationMapper.updateByPrimaryKeySelective(org);
    }

    @Transactional
    public void updateOrganization(SysOrganization org) {
        SysOrganization exists = organizationMapper.selectByPrimaryKey(org.getId());
        if (exists == null) throw new BusinessException("部门不存在");
        exists.setSequence(org.getSequence());
        exists.setDepartment(org.getDepartment());
        exists.setUpdateBy(org.getUpdateBy());
        organizationMapper.updateByPrimaryKeySelective(exists);
    }

    @Transactional
    public void deleteOrganization(int id) {
        OrganizationQo qo = new OrganizationQo();
        qo.setParentId(id);
        if (organizationMapper.selectOrganizationCount(qo) > 0) {
            throw new BusinessException("请先删除下级部门");
        }
        SalesQo sqo = new SalesQo();
        sqo.setOrgId(id);
        if (permissionMapper.selectSalesCount(sqo) > 0) {
            throw new BusinessException("请先迁移部门下的人员");
        }
        organizationMapper.deleteByPrimaryKey(id);
    }
}
